/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {
    var id = $('.user-id').val();

    var ue = UE.getEditor('editor', {initialFrameHeight: 250});

    sendAjax('/v1/manager/user/detail.json', 'get', {user_id: id}, function (json) {
        var role_pic = 'teacher.png';
        switch (json.body.role) {
            case 1: role_pic = 'teacher.png';break;
            case 2: role_pic = 'scientist.png';break;
            case 3: role_pic = 'manager.png';break;
            case 4: role_pic = 'assistant.png';break;
            case 5: role_pic = 'student.png';break;
        }
        $('.img-circle').attr('src', '/img/'+role_pic);
        $('.user-realname').text(json.body.realname);
        $('.user-department').text(json.body.department_name);
        $('.user-number').text(json.body.number);
        $('.user-birthday').text(json.body.birthday);
        $('.user-join-time').text(json.body.join_party_at);
        $('.user-mobile').text(json.body.mobile);
        $('.user-role').text(json.body.role_name);
        $('.user-education').text(json.body.education_name);
        $('.user-score').text(json.body.score);
        var star = '';
        var count = 0;
        if (json.body.score >= 90) {
            count = 5;
        } else if (json.body.score >= 60) {
            count = 3;
        } else {
            count = 0;
        }
        for (var i = 0; i < count; i ++) {
            star += '<i class="fa fa-star"></i>';
        }
        var level = getLevelName(json.body.level);
        $('.user-star').html(level);
        $('.user-summary-text').val(json.body.summary);
    });

    var url = '/v1/manager/advice/to/user.json';
    var data = {
        user_id : id,
        year : 0,
        page : 1,
        page_size : 10
    };
    getDatas(url, data);

    $('.select-year').on('change', function () {
        data.year = $(this).val();
        data.page = 1;
        getDatas(url, data);

    });

    $(document).on('click', '.page-list a', function(){
        data.year = $('.select-year').val();
        data.page = $(this).attr('data-id');
        getDatas(url, data);
    });

    $('#normal-dialog').on('show.bs.modal', function () {
        $(this).find('.modal-header .modal-title').text('查看党员自我评价');
        $(this).find('.modal-body').html('<div>'+$('.user-summary-text').val()+'</div>');
        $(this).find('.modal-footer .btn-primary').addClass('hidden');
    }).on('hide.bs.modal', function () {
        $(this).find('.modal-header .modal-title').text('普通弹窗');
        $(this).find('.modal-body').html('');
        $(this).find('.modal-footer .btn-primary').removeClass('hidden');
    });

    $('#manager-advice').on('show.bs.modal', function () {

    }).on('hide.bs.modal', function(){

        ue.setContent('');
    });

    $('.dialog-btn-save').on('click', function () {
        $('.advice-content').val(ue.getContent());
        var params = {
            user_id: id,
            content: $('.advice-content').val()
        };
        saveData('/v1/manager/save/advice/to/user', {params: params}, '');
    });
});



function getDatas(url, data) {
    sendAjax(url, 'get', data, function (json) {
        var html = '';
        var page_html = '';
        if (json.rows.length > 0) {
            $.each(json.rows, function (n, v) {

                html += '<div class="row"><div class="col-xs-12 data-detail">';
                html += v.department_name == '' ? '<span>党委</span>' : '<span>支部名称：</span><span class="department-name">'+v.department_name+'</span>';
                html += '<span>&nbsp;时间：</span><span class="create-time">'+v.created_at+'</span></div>';
                html += '<div class="col-xs-12"><strong>意见内容：</strong></div>';
                html += '<div class="col-xs-12"><p class="advice-content">'+v.content+'</p></div></div>';
            });
            page_html = pageList(data.page, data.page_size, json.total, json.pages, json.rows.length);
        }
        $('.data-list').html(html);
        $('.page-list').html(page_html);
    });
}